package com.lonty.intelligence.service;

import com.lonty.intelligence.api.Result;
import com.lonty.intelligence.api.ResultCode;
import com.lonty.intelligence.mapper.BuildingMapper;
import com.lonty.intelligence.mapper.CertificateMapper;
import com.lonty.intelligence.mapper.HouseholdMapper;
import com.lonty.intelligence.mapper.OwnerMapper;
import com.lonty.intelligence.model.Building;
import com.lonty.intelligence.model.Certificate;
import com.lonty.intelligence.model.Household;
import com.lonty.intelligence.model.Owner;
import com.lonty.intelligence.util.UUIDUtil;
import com.lonty.intelligence.vo.CertificateVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author 陈璇
 * @Date 2020/4/22 9:24
 * @Version 1.0
 */
@Service
public class CertificateService {

    @Resource
    private CertificateMapper certificateMapper;

    @Resource
    private BuildingMapper buildingMapper;

    @Resource
    private OwnerMapper ownerMapper;

    @Resource
    private HouseholdMapper householdMapper;

    /**
     * 保存房产证
     * @param certificateVo
     * @return
     */
    @Transactional
    public Result saveCertificate(CertificateVo certificateVo){
        Certificate certificate = new Certificate();
        int result = 0;
        //根据ID获取住户信息
        Household household = householdMapper.getById(certificateVo.getHouseholdId());
        //判断楼栋是否存在  不存在则添加楼栋、房产、业主  存在则判断该楼栋业主是否存在并且添加房产信息
        Building getBuilding = new Building();
        BeanUtils.copyProperties(certificateVo, getBuilding);
        Building building = buildingMapper.getByAllInfo(getBuilding);
        //判断房产是否存在 存在则退出，不存在则继续
        if(!certificateIsExit(building.getId())){
            return Result.builder(101, "该房产信息已被添加").build();
        }
        if(building != null){
            //判断业主是否存在  不存在则添加
            Owner getOwner = ownerMapper.getByBuildingId(building.getId());
            if(getOwner == null){
                getOwner = new Owner();
                BeanUtils.copyProperties(household, getOwner);
                getOwner.setId(UUIDUtil.randomID());
                getOwner.setIsDel(0);
                getOwner.setAreaId(building.getAreaId());
                getOwner.setBuildingId(building.getId());
                ownerMapper.saveOwner(getOwner);
            }
            //添加房产信息
            BeanUtils.copyProperties(certificateVo, certificate);
            certificate.setId(UUIDUtil.randomID());
            certificate.setIsDel(0);
            certificate.setBuildingId(building.getId());
            result = certificateMapper.saveCertificate(certificate);
        }else{
            //添加楼栋
            building = new Building();
            BeanUtils.copyProperties(certificateVo, building);
            building.setId(UUIDUtil.randomID());
            building.setIsDel(0);
            buildingMapper.saveBuilding(building);
            //添加房产
            BeanUtils.copyProperties(certificateVo, certificate);
            certificate.setId(UUIDUtil.randomID());
            certificate.setIsDel(0);
            certificate.setBuildingId(building.getId());
            result = certificateMapper.saveCertificate(certificate);
            //添加业主
            Owner owner = new Owner();
            owner.setId(UUIDUtil.randomID());
            owner.setAreaId(certificateVo.getAreaId());
            owner.setBuildingId(building.getId());
            owner.setName(household.getName());
            owner.setTelephone(household.getTelephone());
            owner.setIsLiving(certificateVo.getIsLiving());
            owner.setIsDel(0);
            ownerMapper.saveOwner(owner);
        }
        if(result > 0){
            return Result.builder().data(certificate).build();
        }
        return Result.builder(ResultCode.OPERATION_ERROR).build();
    }

    /**
     * 根据住户ID获取房产信息
     * @param householdId
     * @return
     */
    public List<Certificate> getCertificateByHouseholdId(String householdId){
        return certificateMapper.getCertificateByHouseholdId(householdId);
    }

    /**
     * 根据楼栋ID判断房产是否存在
     * @param buildingId
     * @return
     */
    public boolean certificateIsExit(String buildingId){
        boolean result = false;
        Certificate certificate = certificateMapper.getByBuildingId(buildingId);
        if(certificate == null){
            result = true;
        }
        return result;
    }

}
